package com.api.shopapi.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.MedicationSchedulingDTO;
import com.api.shopapi.mapper.MedicationSchedulingMapper;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service("MedicationSchedulingServiceImplOne")
public class MedicationSchedulingServiceImpl implements MedicationSchedulingService {

    @Autowired
    private MedicationSchedulingMapper medicationSchedulingMapper;


    @Autowired
    @Qualifier("MedicationSchedulingMorningServiceImplOne")
    private MedicationSchedulingMorningService medicationSchedulingMorningService;

    @Autowired
    @Qualifier("MedicationSchedulingNoonServiceImplOne")
    private MedicationSchedulingNoonService medicationSchedulingNoonService;

    @Autowired
    @Qualifier("MedicationSchedulingNightServiceImplOne")
    private MedicationSchedulingNightService medicationSchedulingNightService;


    @Autowired
    @Qualifier("MedicationSchedulingMorningItemServiceImplOne")
    private MedicationSchedulingMorningItemService medicationSchedulingMorningItemService;

    @Autowired
    @Qualifier("MedicationSchedulingNoonItemServiceImplOne")
    private MedicationSchedulingNoonItemService medicationSchedulingNoonItemService;

    @Autowired
    @Qualifier("MedicationSchedulingNightItemServiceImplOne")
    private MedicationSchedulingNightItemService medicationSchedulingNightItemService;


    @Override
    @Transactional
    public int insert(MedicationScheduling medicationScheduling,JSONObject JOscheduling) {
        String medicationSchedulingCode = medicationScheduling.getMedicationSchedulingCode();
        int count = medicationSchedulingMapper.insert(medicationScheduling);
        addSchedulingPlan(JOscheduling,medicationSchedulingCode);
        return count;
    }

    private void addSchedulingPlan(JSONObject JOscheduling, String medicationSchedulingCode) {
        //早上
        JSONArray morningPlanStr = JOscheduling.getJSONArray("morningPlan");
        if (morningPlanStr.size() > 0) {
            addMorning(morningPlanStr, medicationSchedulingCode);
        }
        //中午
        JSONArray noonPlanStr = JOscheduling.getJSONArray("noonPlan");
        if (noonPlanStr.size() > 0) {
            addNoon(noonPlanStr, medicationSchedulingCode);
        }
        //晚上
        JSONArray nightPlanStr = JOscheduling.getJSONArray("nightPlan");
        if (nightPlanStr.size() > 0) {
            addNight(nightPlanStr, medicationSchedulingCode);
        }
    }




    private void addMorning(JSONArray morningPlanStr, String medicationSchedulingCode) {
        MedicationSchedulingMorning medicationSchedulingMorning = new MedicationSchedulingMorning();
        String medicationSchedulingMorningCode = UUID.randomUUID().toString();
        medicationSchedulingMorning.setMedicationSchedulingMorningCode(medicationSchedulingMorningCode);
        medicationSchedulingMorning.setMedicationSchedulingCode(medicationSchedulingCode);
        medicationSchedulingMorningService.insert(medicationSchedulingMorning);

        MedicationSchedulingMorningItem medicationSchedulingMorningItem = new MedicationSchedulingMorningItem();
        for (Object i :morningPlanStr){
            medicationSchedulingMorningItem.setMedicationSchedulingMorningItemCode(UUID.randomUUID().toString());
            medicationSchedulingMorningItem.setMedicationSchedulingMorningCode(medicationSchedulingMorningCode);
            medicationSchedulingMorningItem.setName((String) i);
            medicationSchedulingMorningItemService.insert(medicationSchedulingMorningItem);
        }
    }

    private void addNoon(JSONArray noonPlanStr, String medicationSchedulingCode) {
        MedicationSchedulingNoon medicationSchedulingNoon = new MedicationSchedulingNoon();
        String medicationSchedulingNoonCode = UUID.randomUUID().toString();
        medicationSchedulingNoon.setMedicationSchedulingNoonCode(medicationSchedulingNoonCode);
        medicationSchedulingNoon.setMedicationSchedulingCode(medicationSchedulingCode);
        medicationSchedulingNoonService.insert(medicationSchedulingNoon);

        MedicationSchedulingNoonItem medicationSchedulingNoonItem = new MedicationSchedulingNoonItem();
        for (Object i :noonPlanStr){
            medicationSchedulingNoonItem.setMedicationSchedulingNoonItemCode(UUID.randomUUID().toString());
            medicationSchedulingNoonItem.setMedicationSchedulingNoonCode(medicationSchedulingNoonCode);
            medicationSchedulingNoonItem.setName((String) i);
            medicationSchedulingNoonItemService.insert(medicationSchedulingNoonItem);
        }
    }

    private void addNight(JSONArray nightPlanStr, String medicationSchedulingCode) {
        MedicationSchedulingNight medicationSchedulingNight = new MedicationSchedulingNight();
        String medicationSchedulingNightCode = UUID.randomUUID().toString();
        medicationSchedulingNight.setMedicationSchedulingNightCode(medicationSchedulingNightCode);
        medicationSchedulingNight.setMedicationSchedulingCode(medicationSchedulingCode);
        medicationSchedulingNightService.insert(medicationSchedulingNight);

        MedicationSchedulingNightItem medicationSchedulingNightItem = new MedicationSchedulingNightItem();
        for (Object i :nightPlanStr){
            medicationSchedulingNightItem.setMedicationSchedulingNightItemCode(UUID.randomUUID().toString());
            medicationSchedulingNightItem.setMedicationSchedulingNightCode(medicationSchedulingNightCode);
            medicationSchedulingNightItem.setName((String) i);
            medicationSchedulingNightItemService.insert(medicationSchedulingNightItem);
        }
    }


    @Override
    @Transactional
    public int updateByMedicationSchedulingCode(MedicationScheduling medicationScheduling,JSONObject JOscheduling) {
        String medicationSchedulingCode = medicationScheduling.getMedicationSchedulingCode();
        //先删除数据
        MedicationSchedulingMorning medicationSchedulingMorning = medicationSchedulingMorningService.getByMedicationSchedulingCode(medicationSchedulingCode);
        if(medicationSchedulingMorning!=null){
            medicationSchedulingMorningItemService.deleteByMedicationSchedulingMorningCode(medicationSchedulingMorning.getMedicationSchedulingMorningCode());
            medicationSchedulingMorningService.deleteByMedicationSchedulingMorningCode(medicationSchedulingMorning.getMedicationSchedulingMorningCode());
        }


        MedicationSchedulingNoon medicationSchedulingNoon = medicationSchedulingNoonService.getByMedicationSchedulingCode(medicationSchedulingCode);
        if(medicationSchedulingNoon!=null){
            medicationSchedulingNoonItemService.deleteByMedicationSchedulingNoonCode(medicationSchedulingNoon.getMedicationSchedulingNoonCode());
            medicationSchedulingNoonService.deleteByMedicationSchedulingNoonCode(medicationSchedulingNoon.getMedicationSchedulingNoonCode());
        }



        MedicationSchedulingNight medicationSchedulingNight = medicationSchedulingNightService.getByMedicationSchedulingCode(medicationSchedulingCode);
        if(medicationSchedulingNight!=null){
            medicationSchedulingNightItemService.deleteByMedicationSchedulingNightCode(medicationSchedulingNight.getMedicationSchedulingNightCode());
            medicationSchedulingNightService.deleteByMedicationSchedulingNightCode(medicationSchedulingNight.getMedicationSchedulingNightCode());
        }

        addSchedulingPlan(JOscheduling,medicationSchedulingCode);
        return medicationSchedulingMapper.updateByMedicationSchedulingCode(medicationScheduling);
    }

    @Override
    public int deleteByMedicationSchedulingCode(String medicationSchedulingCode) {
        return medicationSchedulingMapper.deleteByMedicationSchedulingCode(medicationSchedulingCode);
    }

    @Override
    public MedicationScheduling getByMedicationSchedulingCode(String medicationSchedulingCode) {
        return medicationSchedulingMapper.getByMedicationSchedulingCode(medicationSchedulingCode);
    }

    @Override
    public MedicationScheduling getByTimeAndElderlyCode(Date time,String elderlyCode) {
        return medicationSchedulingMapper.getByTimeAndElderlyCode(time,elderlyCode);
    }

    @Override
    public PageInfo<MedicationSchedulingDTO> getPageList(int pageIndex, int pageSize, String institutionCode, String elderlyName, int status, String startTime, String endTime) {
        PageHelper.startPage(pageIndex, pageSize);
        List<MedicationSchedulingDTO> medicationSchedulingDTOList = medicationSchedulingMapper.getPageList(institutionCode,elderlyName,status,startTime,endTime);
        PageInfo<MedicationSchedulingDTO> pageInfo = new PageInfo<>(medicationSchedulingDTOList);
        return pageInfo;
    }

    @Override
    public
    PageInfo<MedicationSchedulingDTO> pageByGovCode(int pageIndex, int pageSize, String governmentCode){
        PageHelper.startPage(pageIndex, pageSize);
        List<MedicationSchedulingDTO> medicationSchedulingDTOList = medicationSchedulingMapper.listByGovCode(governmentCode);
        PageInfo<MedicationSchedulingDTO> pageInfo = new PageInfo<>(medicationSchedulingDTOList);
        return pageInfo;
    }

}
